package com.jiahao.material.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiahao.material.entity.*;
import com.jiahao.material.mapper.WarehouseMaterialMapper;
import com.jiahao.material.services.IDeliveryService;
import com.jiahao.material.services.IRoleService;
import com.jiahao.material.utils.ResultUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/delivery")
public class DeliveryController {

    @Autowired
    private IDeliveryService deliveryService;

    @Autowired
    private WarehouseMaterialMapper warehouseMaterialMapper;


    @RequiresPermissions("addDelivery")
    @PostMapping("/add")
    public Result add(@RequestBody Delivery delivery) throws Exception{
        delivery.setStatus("02");
        deliveryService.save(delivery);
        return ResultUtil.success("添加出库成功!");
    }

    @GetMapping("/byId/{id}")
    public Result getById(@PathVariable String id) throws Exception{
        Delivery byId = deliveryService.getById(id);
        return ResultUtil.success("",byId);
    }

    @PostMapping("/page")
    public Result getByPage(@RequestBody Map<String,Object> map) throws Exception{
        int pageSize = map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString());
        int currentPage = map.get("currentPage") == null ? 1 : Integer.parseInt(map.get("currentPage").toString());
        Page<Delivery> page = new Page<>(currentPage,pageSize);

        QueryWrapper<Delivery> deliveryQueryWrapper = new QueryWrapper<>();

        //根据需求添加查询条件
        if (map.get("warehouseId")!=null && !map.get("warehouseId").toString().equals("")){
            deliveryQueryWrapper.eq("warehouseId",map.get("warehouseId").toString());
        }

        if (map.get("dcode")!=null && !map.get("dcode").toString().equals("")){
            deliveryQueryWrapper.like("dcode",map.get("dcode").toString());
        }

        if (map.get("customerId")!=null && !map.get("customerId").toString().equals("")){
            deliveryQueryWrapper.eq("customerId",map.get("customerId").toString());
        }

        if (map.get("materialId")!=null && !map.get("materialId").toString().equals("")){
            deliveryQueryWrapper.eq("materialId",map.get("materialId").toString());
        }
        if (map.get("status")!=null && !map.get("status").toString().equals("")){
            deliveryQueryWrapper.eq("status",map.get("status").toString());
        }
        IPage<Delivery> page1 = deliveryService.page(page, deliveryQueryWrapper);
        return ResultUtil.success("",page1);
    }

    @RequiresPermissions("updateDelivery")
    @PutMapping("/update")
    @Transactional
    public Result update(@RequestBody Delivery delivery) throws Exception{
        //如果出库通过,则进行仓库数量的增加减
        updateCount(delivery);
        deliveryService.updateById(delivery);
        return ResultUtil.success("更新出库成功!");
    }

    @RequiresPermissions("delDelivery")
    @DeleteMapping("/del/{id}")
    public Result delById(@PathVariable String id) throws Exception{
        deliveryService.removeById(id);
        return ResultUtil.success("删除出库成功!");
    }

    @RequiresPermissions("delDelivery")
    @PostMapping("/dels")
    public Result delByIds(@RequestBody List<String> ids) throws Exception{
        deliveryService.removeByIds(ids);
        return ResultUtil.success("删除出库成功!");
    }

    private void updateCount(Delivery delivery) throws Exception{
        if (delivery.getStatus().equals("01")){
            String wid = delivery.getWarehouseId();
            String mid = delivery.getMaterialId();

            //查询是否第一次通过
            Delivery byId = deliveryService.getById(delivery.getId());
            if (!byId.getStatus().equals("01")){
                //查询库存数量
                QueryWrapper<WarehouseMaterial> warehouseMaterialQueryWrapper = new QueryWrapper<>();
                warehouseMaterialQueryWrapper.eq("wid",wid);
                warehouseMaterialQueryWrapper.eq("mid",mid);
                WarehouseMaterial warehouseMaterial = warehouseMaterialMapper.selectOne(warehouseMaterialQueryWrapper);
                int i = warehouseMaterial.getCount() - delivery.getCount();
                if (i<0){
                    throw new Exception();
                }
                UpdateWrapper<WarehouseMaterial> warehouseMaterialUpdateWrapper = new UpdateWrapper<>();
                warehouseMaterialUpdateWrapper.eq("wid",wid);
                warehouseMaterialUpdateWrapper.eq("mid",mid);
                WarehouseMaterial warehouseMaterial1 = new WarehouseMaterial();
                warehouseMaterial1.setCount(i);
                warehouseMaterial1.setMid(mid);
                warehouseMaterial1.setWid(wid);
                warehouseMaterialMapper.update(warehouseMaterial1,warehouseMaterialUpdateWrapper);
            }
        }

    }

}
